package cn.b.sky.mes.mm.service.impl;

import java.util.List;

import javax.annotation.Resource;

import cn.b.sky.mes.mm.dao.DistributionTriggerModeDao;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.b.sky.mes.mm.model.DistributionTriggerMode;
import cn.b.sky.mes.mm.model.condition.DistributionTriggerModeCondition;
import cn.b.sky.mes.mm.service.DistributionTriggerModeService;
import cn.b.sky.common.model.BatchJobReport;
import cn.b.sky.common.utils.DatabaseUtils;
import cn.b.sky.i18n.utils.LocaleResourceBundle;
import cn.b.sky.user.SecurityContextUtil;

/**
 * 
 * <strong>Title : DistributionTriggerModeServiceImpl.java<br></strong>
 * <strong>Package : cn.b.sky.mes.cm.service.impl<br></strong>
 * <strong>Description : </strong>配送需求触发策略业务实现<br> 
 * <strong>Create on : 2016年9月9日 下午1:25:48<br></strong>
 * <p>
 * @author zhaoxin01<br>
 * @version <strong>v1.0.0</strong><br>
 * <br>
 * <strong>修改历史:</strong><br>
 * 修改人|修改日期|修改描述<br>
 * -------------------------------------------<br>
 * <br>
 * <br>
 */
@Service("distributionTriggerModeService")
public class DistributionTriggerModeServiceImpl implements DistributionTriggerModeService{

  private static final Logger LOG = LoggerFactory.getLogger(DistributionTriggerModeServiceImpl.class);
  
  @Resource
  DistributionTriggerModeDao distributionTriggerModeDao;
  
  @Override
  public List<DistributionTriggerMode> selectDistributionTriggerModeByCondition(
      DistributionTriggerModeCondition distributionTriggerModeCondition) {
    if(distributionTriggerModeCondition==null){
      return null;
    }
    return distributionTriggerModeDao.selectDistributionTriggerModeByCondition(distributionTriggerModeCondition);
  }

  @Override
  public int selectDistributionTriggerModeCount(DistributionTriggerModeCondition distributionTriggerModeCondition) {
    if(distributionTriggerModeCondition==null){
      return 0;
    }
    return distributionTriggerModeDao.selectDistributionTriggerModeCount(distributionTriggerModeCondition);
  }

  @Override
  public DistributionTriggerMode selectDistributionTriggerMode(String siteCode, String distributionTriggerModeCode) {
    if (StringUtils.isBlank(siteCode) || StringUtils.isBlank(distributionTriggerModeCode)) {
      return null;
    }
    return distributionTriggerModeDao.selectDistributionTriggerMode(siteCode, distributionTriggerModeCode);
  }

  @Override
  public void insertDistributionTriggerMode(DistributionTriggerMode DistributionTriggerMode) {
    if(DistributionTriggerMode==null){
      return;
    }
    distributionTriggerModeDao.insertDistributionTriggerMode(DistributionTriggerMode);
  }

  @Override
  public void updateDistributionTriggerMode(DistributionTriggerMode DistributionTriggerMode) {
    if(DistributionTriggerMode==null){
      return;
    }
    distributionTriggerModeDao.updateDistributionTriggerMode(DistributionTriggerMode);
  }

  @Override
  public BatchJobReport deleteDistributionTriggerMode(String siteCode, String distributionTriggerModeCode) {
    if (StringUtils.isEmpty(siteCode) || StringUtils.isEmpty(distributionTriggerModeCode)) {
      return new BatchJobReport("", "siteCode or distributionTriggerModeCode is null", false);
    }
    String message = distributionTriggerModeCode;
    boolean success = true;
    String cause = "";

    try {
      distributionTriggerModeDao.deleteDistributionTriggerMode(siteCode, distributionTriggerModeCode);
    } catch (Exception e) {
      LOG.error("{}", e);
      success = false;
      String errorMsg = e.getMessage();
      if (DatabaseUtils.isConstraintsError(e.getMessage())) {
        //外键删除已存在提示
        errorMsg = LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),SecurityContextUtil.getLanguageCode(), "dataBaseOperationPkFail.M")
            + LocaleResourceBundle.getCommonResourceLabel(SecurityContextUtil.getSiteCode(),SecurityContextUtil.getLanguageCode(), "notDelete.M");
      }
      cause = errorMsg;
    }
    return new BatchJobReport(message, cause, success);
  }

  @Override
  public List<DistributionTriggerMode> selectDistributionTriggerModeByLockFlag(String siteCode, Integer lockFlag) {
    if (!StringUtils.isBlank(siteCode) && lockFlag!=null) {
      return distributionTriggerModeDao.selectDistributionTriggerModeByLockFlag(siteCode, lockFlag);
    }
    return null;
  }

}
